package com.daiziru.wuyanmall.controller;

import com.daiziru.wuyanmall.entity.Users;
import com.daiziru.wuyanmall.service.UserService;
import com.daiziru.wuyanmall.vo.ResultStatus;
import com.daiziru.wuyanmall.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

//@Controller
//@ResponseBody//异步请求，返回的是json数据
@RestController
@RequestMapping("/user")
@Api(value = "提供用户登录和注册的接口", tags = "用户管理")
@CrossOrigin//允许跨域访问
public class UserController {

    @Resource
    private UserService userService;

    @ApiOperation(value="用户登录接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string",name = "username", value = "⽤户登录账号",required = true),
            @ApiImplicitParam(dataType = "string",name = "password", value = "⽤户登录密码",required = true)
    })
    @GetMapping("/login")
    public ResultVO login(@RequestParam(value = "username") String name,
                          @RequestParam(value = "password") String pwd) {
        ResultVO resultVO = userService.checkLogin(name, pwd);

        return resultVO;
    }

    @ApiOperation(value="用户注册接口")//生成后端接口文档
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "string",name = "username", value = "⽤户注册账号",required = true),
            @ApiImplicitParam(dataType = "string",name = "password", value = "⽤户注册密码",required = true)
    })
    @PostMapping("/regist")
    public ResultVO regist(@RequestBody Users users) {
        ResultVO resultVO = userService.userResgit(users.getUsername(), users.getPassword());
        return resultVO;
    }

    @ApiOperation("校验token是否过期接⼝")
    @GetMapping("/check")
    public ResultVO userTokencheck(@RequestHeader("token") String token){
        return new ResultVO(ResultStatus.OK,"success",null);
    }
}
